package maassvc

import (
	corev1 "gluenet/internal/agent/api/core/v1"
	"gluenet/utils/rpc/v2/define"
	"net/url"

	gomaasclient "github.com/maas/gomaasclient/client"
)

//This part of the rpc module is used to operate virtual machines in the srvm.
//The documentation can refer to the maas or srvm documentation.

func (svc *Service) BootResourcesSearch(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("boot-resources").Get("", url.Values{}, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) SubNetsSearch(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("subnets").Get("", url.Values{}, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) VMHostSearch(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("vm-hosts").GetSubObject(maasReq.GetParams).Get("", url.Values{}, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) VMHostsSearch(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("vm-hosts").Get("", url.Values{}, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) VMHostsCreate(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	val := url.Values{}
	for i := range maasReq.Value {
		val.Set(i, maasReq.Value[i])
	}
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("vm-hosts").GetSubObject(maasReq.GetParams).Post("compose", val, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) VMHostsDelete(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("vm-hosts").GetSubObject(maasReq.GetParams).Delete()
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) MachinesCreate(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	val := url.Values{}
	for i := range maasReq.Value {
		val.Set(i, maasReq.Value[i])
	}
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("machines").Post("", val, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) MachinesDelete(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("machines").GetSubObject(maasReq.GetParams).Delete()
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) MachinesUpdate(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	val := url.Values{}
	for i := range maasReq.Value {
		val.Set(i, maasReq.Value[i])
	}
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("machines").GetSubObject(maasReq.GetParams).Put(val, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) MachineSearch(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("machines").GetSubObject(maasReq.GetParams).Get("", url.Values{}, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) MachineDetail(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("machines").GetSubObject(maasReq.GetParams).Get("details", url.Values{}, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) MachinesSearch(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("machines").Get("", url.Values{}, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) MachinesPowerOn(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	val := url.Values{}
	for i := range maasReq.Value {
		val.Set(i, maasReq.Value[i])
	}
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("machines").GetSubObject(maasReq.GetParams).Post("power_on", val, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) MachinesPowerOff(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	val := url.Values{}
	for i := range maasReq.Value {
		val.Set(i, maasReq.Value[i])
	}
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("machines").GetSubObject(maasReq.GetParams).Post("power_off", val, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) TagsUpdateNodes(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	val := url.Values{}
	for i := range maasReq.Value {
		val.Set(i, maasReq.Value[i])
	}
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("tags").GetSubObject(maasReq.GetParams).Post("update_nodes", val, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) MachineDeploy(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	val := url.Values{}
	for i := range maasReq.Value {
		val.Set(i, maasReq.Value[i])
	}
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("machines").GetSubObject(maasReq.GetParams).Post("deploy", val, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}

func (svc *Service) SubNetsIPAddressesSearch(maasReq define.MaasReq) corev1.CommonResp {
	var data []byte
	val := url.Values{}
	for i := range maasReq.Value {
		val.Set(i, maasReq.Value[i])
	}
	c, _ := gomaasclient.GetApiClient(maasReq.MaasURL, maasReq.APIKey, maasReq.Version)
	err := c.GetSubObject("subnets").GetSubObject(maasReq.GetParams).Get("ip_addresses", url.Values{}, func(bytes []byte) error {
		data = bytes
		return nil
	})
	if err != nil {
		goto ErrProcess
	}
	return corev1.NewCommonSuccessRespWithData(data)
ErrProcess:
	svc.logger.Errorf(err.Error())
	return corev1.NewCommonFailureResp(err.Error())
}
